<template>
  <div class="p-2">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="编码" prop="matCode">
              <el-input v-model="queryParams.matCode" placeholder="请输入物料编码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="分类" prop="matClass">
              <el-select v-model="queryParams.matClass" placeholder="请选择物料分类" clearable filterable>
                <el-option
                  v-for="item in classList"
                  :key="item.classCode"
                  :label="item.className"
                  :value="item.classCode"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          size="mini"
          @click="handleSelectArr"
        >选择</el-button>
      </el-col>
    </el-row>
    <el-card shadow="never">
      <el-table ref="selectMatTable"  v-loading="loading" :data="matLabelList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" :selectable="checkSelectable" align="center" />
        <el-table-column label="主键" align="center" prop="labelId" v-if="false" />
        <el-table-column label="物料编码" fixed align="center" prop="matCode" :show-overflow-tooltip="true" min-width="100"/>
        <el-table-column label="物料描述" fixed align="center" prop="matName" :show-overflow-tooltip="true" width="120"/>
        <el-table-column label="财务编码" align="center" prop="fdCode" :show-overflow-tooltip="true" width="120"/>
        <el-table-column label="图号" align="center" prop="figNum" :show-overflow-tooltip="true" width="120"/>
        <el-table-column label="物料组" align="center" prop="matGroup" :show-overflow-tooltip="true" width="120"/>
        <el-table-column label="分类" align="center" prop="matClass" :show-overflow-tooltip="true" width="120"/>
        <el-table-column label="基本单位" align="center" prop="unitCode" :show-overflow-tooltip="true" width="120"/>
        <el-table-column label="批次" align="center" prop="batch" :show-overflow-tooltip="true" width="180"/>
        <el-table-column label="供应商编码" align="center" prop="supplierCode" :show-overflow-tooltip="true" width="120"/>
        <el-table-column label="供应商名称" align="center" prop="supplierName" :show-overflow-tooltip="true" width="150"/>
        <el-table-column label="生产时间" align="center" prop="prodTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.prodTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="数量" align="center" prop="quantity" />
        <el-table-column label="可用数量" align="center" prop="usableQuantity" />
        <el-table-column label="已领取数量" align="center" prop="receivedQuantity" width="120"/>
        <el-table-column label="单价" align="center" prop="unitPrice" />
        <el-table-column label="入库单号" align="center" prop="orderNo" :show-overflow-tooltip="true" width="150"/>
        <el-table-column label="操作" fixed="right"  align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button v-if="labelIdArr.indexOf(scope.row.labelId) < 0" size="small" type="text" icon="Select" @click="handleSelect(scope.row)">选择</el-button>
            <span v-if="labelIdArr.indexOf(scope.row.labelId) >= 0">已选</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="SelectMatLable" lang="ts">
import { MatQuery } from '@/api/wmscloud/mat/types';
import { MatClassVO } from '@/api/wmscloud/matClass/types';
import { listAllClass } from '@/api/wmscloud/matClass';
import { ElTable } from 'element-plus';
import { parseTime } from '@/utils/ruoyi';
import { MatLabelVO } from '@/api/wmscloud/matLabel/types';
import { listMatLabelDialog } from '@/api/wmscloud/matLabel';

const matLabelList = ref<MatLabelVO[]>([]);
const classList = ref<MatClassVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);

// 或者从 父组件中 中接收 labelIdArr
const props = defineProps<{
  labelIdArr: (string | number)[];
}>();

const queryFormRef = ref<ElFormInstance>();
const selectMatTable = ref<InstanceType<typeof ElTable>>();
const queryParams = reactive<MatQuery>({
    pageNum: 1,
    pageSize: 10,
    matCode: undefined,
    matName: undefined,
    fdCode: undefined,
    figNum: undefined,
    matGroup: undefined,
    matClass: undefined,
    params: {
    }
});
/** 查询物料主数据列表 */
const getList = async () => {
  loading.value = true;
  const res = await listMatLabelDialog(queryParams);
  matLabelList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 加载所有物料分类 */
const listAllClasss = async () => {
  const res = await listAllClass();
  classList.value = res.data;
}

/** 定义单选和多选的emit事件 */
const emit = defineEmits<{
  (e: 'confirmSelect', row: MatLabelVO): void;
  (e: 'confirmSelectArr', rows: MatLabelVO[]): void;
}>();

/** 单选物料 */
const handleSelect = (row: MatLabelVO) => {
  const table = selectMatTable.value;
  if (table?.clearSelection) table.clearSelection();
  emit('confirmSelect', row);
};

/** 多选物料 */
const handleSelectArr = () => {
  const table = selectMatTable.value;
  const selectedRows: MatLabelVO[] = (table?.getSelectionRows?.() ?? []);
  if (table?.clearSelection) table.clearSelection();
  emit('confirmSelectArr', selectedRows);
};
/** 多选框选中数据 */
const handleSelectionChange = (selection: MatLabelVO[]) => {
  ids.value = selection.map(item => item.labelId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 是否可选 */
const checkSelectable = (row: MatLabelVO) => {
  return !props.labelIdArr.includes(row.labelId);
};

defineExpose({
  getList
})
onMounted(() => {
  getList();
  listAllClasss();
});
</script>
